package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")


public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;


    //添加方法
    @RequestMapping("/add.do")
    public Result add(@RequestBody CheckGroup checkGroup,Integer [] checkitemIds){
        try {
            int count = checkGroupService.add(checkGroup, checkitemIds);
            if (count > 0){
                return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
            }else {
                return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL,"网络繁忙，请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL,e.getMessage());
        }
    }
    //分页查询
    @RequestMapping("/findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = checkGroupService.findPage(queryPageBean);
            return new Result(true,"查询数据成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询数据失败","网络繁忙，请稍后再试");
        }
    }

    //编辑回显窗口
    @RequestMapping("/findById/{id}.do")
    public Result findById(@PathVariable Integer id){
        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            return new Result(true,"查询数据成功",checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询数据失败","网络繁忙，请稍后再试");
        }
    }

    //编辑检查组
    @RequestMapping("/edit.do")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer [] checkitemIds){
        try {
            int count = checkGroupService.edit(checkGroup, checkitemIds);
            if (count > 0){
                return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
            }else {
                return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL,"网络繁忙，请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }
    //展示所有检查组
    @RequestMapping("/findAll.do")
    public Result findAll(){


        System.out.println("==================");

        List<CheckGroup> checkGroupList = checkGroupService.findAll();
        if (checkGroupList != null && checkGroupList.size() >0){
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        }else {
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/deleteById/{id}.do")
    public Result deleteById(@PathVariable Integer id) {
        System.out.println("id = " + id);
        try {
            int count = checkGroupService.deleteById(id);
            if (count < 0){
                return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL, "网络繁忙，请稍后再试");
            }
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL,e.getMessage());
        }
    }
}
